//
//  Contact1TableViewController.m
//  test2
//
//  Created by 贺银银 on 16/3/26.
//  Copyright © 2016年 hyy. All rights reserved.
//

#import "Contact1TableViewController.h"
#import <CDChatManager.h>
#import "ChatRoomViewController.h"
#import "AddContactViewController.h"
#import "ContactTableViewCell.h"
#import "LoginViewController.h"
#import "SVProgressHUD.h"
#import "ContactDetailViewController.h"
@interface Contact1TableViewController ()
@property (nonatomic,strong) UITextField *userNameTextField;
@property (nonatomic,strong) NSMutableArray *userArray;


@end

@implementation Contact1TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tiaoAction) name:@"tiao" object:nil];
    
    
}
- (void)tiaoAction{
    self.tabBarController.selectedIndex = 0;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSArray *)findAllUser{
    self.userArray = [[NSMutableArray alloc]init];

    AVUser *user = [AVUser currentUser];
    
    
    AVQuery *query1 = [AVQuery queryWithClassName:@"_User"];
    AVQuery *query2 = [AVQuery queryWithClassName:@"_User"];
    [query1 whereKey:@"username" notEqualTo:@""];
    [query2 whereKey:@"username" notEqualTo:user.username];
    AVQuery *query = [AVQuery andQueryWithSubqueries:@[query1,query2]];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects.count > 0) {
            NSArray *array = [NSArray arrayWithArray:objects];
            AVQuery *query3 = [AVQuery queryWithClassName:@"_Followee"];
            [query3 whereKey:@"objectId" notEqualTo:@""];
            [query3 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                
                for (int i = 0; i < array.count; i++) {
                    
                    AVUser *user1 = array[i];
                    
                    for (AVUser *user2 in objects) {
                        
                        if ([user1.objectId isEqual:user2[@"followee"][@"objectId"] ]) {
                            
                            [self.userArray addObject:user1.username];
                        }
                    }
                    
                }
            
                [self.tableView reloadData];
            }];
            
            
            
        }else{
            
            NSLog(@"%@",error);
        }
    }];

    return self.userArray;
}

- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.tabBarController.tabBar.hidden = NO;
//    if (self.userArray.count == 0) {
    
        [self findAllUser];
//    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 2;
    }else{
        return self.userArray.count;

    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ContactTableViewCell *cell = [[ContactTableViewCell alloc]init];
    
    NSArray *array = @[@"添加好友",@"退出登录"];
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"addContact" forIndexPath:indexPath];
        cell.handleNameLabel.text = array[indexPath.row];
        
    }else if(indexPath.section == 0 && indexPath.row == 1){
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"logout" forIndexPath:indexPath];
        cell.logoutLabel.text = array[indexPath.row];
    }
    else{
        if (self.userArray.count != 0) {
            
            cell = [tableView dequeueReusableCellWithIdentifier:@"contact" forIndexPath:indexPath];
            cell.userNameLabel.text = self.userArray[indexPath.row];
        }
        
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        AddContactViewController *addContactVC = [[AddContactViewController alloc]init];
        addContactVC.userArray = [NSMutableArray array];
        [addContactVC.userArray addObjectsFromArray:self.userArray];
        NSLog(@"%@",addContactVC.userArray);
        
    }
    else if (indexPath.section == 0 && indexPath.row == 1) {
        
        AVUser *user = [AVUser currentUser];
        if (user) {
            
            [AVUser logOut];
            [SVProgressHUD showSuccessWithStatus:@"退出成功"];
        }else{
            [SVProgressHUD showErrorWithStatus:@"退出失败"];
        }
        
    }
//    else if (indexPath.section == 1) {
//
//        ContactDetailViewController *contactDVC = [[ContactDetailViewController alloc]init];
//        contactDVC.userName = self.userArray[indexPath.row];
//        NSLog(@"%@",contactDVC.userName);
//    }
    
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    if ([segue.identifier isEqualToString:@"addContact"]) {
        
        
        [segue.destinationViewController setValue:self.userArray forKey:@"userArray"];
    }
    
//    AddContactViewController *addContactVC = [[AddContactViewController alloc]init];
//    addContactVC.userArray  = self.userArray;
//    NSLog(@"%@",addContactVC.userArray);
    if ([segue.identifier isEqualToString:@"contactDetail"]) {
        
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        if (indexPath.section == 0 && indexPath.row == 1) {
            
            [self.userArray removeAllObjects];
            
            
            
        }else if (indexPath.section == 1){
            
            if (self.userArray.count != 0) {
                
                [segue.destinationViewController setValue:self.userArray[indexPath.row] forKey:@"userName"];
            }
        }
        
    }
    
    
    
    
}


@end
